package cn.gov.anhai.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 */
public class LoginUtil {
    /**
     * 得到token
     */
    public static String getToken(Long uid) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("date", new Date().getTime() + "");
        return TokenGenerator.getToken(map);
    }

    /**
     * 得到uid
     */
    public static Long getToken(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null) return null;
        token = token.replaceFirst("Bearer ", "");
        Map<String, String> data = TokenGenerator.getInfo(token);
        if (data == null) return null;
        if (!MyUtil.hasMapKey(data, "uid")) return null;
        try {
            return Long.parseLong(data.get("uid"));
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        System.out.println(getToken(195336335861565145L));
    }
}
